<?php

/**
 * 栏目分类 模型
 *
 * Class Category
 * @Space app\common\model
 * @Author pp <124910168@qq.com>
 * @Date 21th Aug 2018
 */

namespace app\common\model;

use think\Model;

class Article extends Model{


    /**
     * 根据文章分类ID获取文章列表
     * @param int $ac_id 文章分类ID
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getArticlesByClassId($ac_id = 0)
    {
        $condition['article_show'] = 1;
        if ($ac_id) {
            $condition['ac_id'] = $ac_id;
        }
        $data = M('article')->where('ac_id', $ac_id)->order('article_sort asc')->select();
        return $data;
    }

    public function getArticleDetailById($aid)
    {
        $data = M('article')->where('article_id', $aid)->find();
        return $data;
    }

    /**
     * 获取所有单页文章
     *
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getArticleList()
    {
        return M('article')->where('article_show', 1)->order('article_sort asc')->field('article_id, article_title')->select();
    }

    /** 返回某个字段的值
     * @param $where 查询条件
     * @param string $field 查询字段
     * @return mixed
     */
    public function getValue($where, $field="*"){
        return $this->where($where)->value($field);
    }

    /** 查询某一列的值 返回数组
     * @param $where 查询条件
     * @param string $field 指定索引
     * @return mixed
     */
    public function getColumn($where, $field="*"){
        return $this->where($where)->column($field);
    }

  /**查询一条数据
     * @param $where
     * @param string $field
     * @return array|false|\PDOStatement|string|Model
     */
  public function getOneDarry($where, $field="*"){
      $returndata = [];
      $data = $this->where($where)->field($field)->find();
      if(!empty($data)){
          $returndata = $data->toArray();
      }
      return $returndata;
  }
  
  //列表数据
  public function getLists($where = 1, $order = ['id'], $field = "*", $limit = 20){
     $lists = $this->where($where)->order($order)->field($field)->limit($limit)->select();
     $return_lists = [];
     foreach ($lists as $k => $v){
         $return_lists[$k] = $v->toArray();
     }
     return $return_lists;
  }

  //分页数据
  public function getPageLists($where = 1, $order = ['id'], $field = "*", $p = 20){
    $lists = $this->where($where)->order($order)->paginate(config('pageSize') ?? $p);
    $page = $lists->render();
    $return_lists = [];
   foreach ($lists as $k => $v){
       $return_lists[$k] = $v->toArray();
   }
   return [$return_lists, $page];
  }
}














